package com.xbongbong.pro.statistic.help.strategy.field.method;

import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.statistic.help.FieldAnalysisHelp;
import com.xbongbong.pro.statistic.help.strategy.field.FieldAnalysisStrategy;
import com.xbongbong.pro.statistic.pojo.FieldAnalysisPojo;
import com.xbongbong.pro.statistic.result.pojo.FieldAttrPojo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhouwq
 * @date 2020/9/8 9:58
 */

@Component
public class TeamFieldAnalysis implements FieldAnalysisStrategy {
    @Resource
    public FieldAnalysisHelp fieldAnalysisHelp;
    @Override
    public Map<String, String> doAnalysisField(FieldAnalysisPojo fieldAnalysisPojo) throws XbbException {
        Map<String, String> results = new HashMap<>(1 << 4);
        FieldAttrPojo fieldAttrPojo = fieldAnalysisPojo.getField();
        ChartEntity chartEntity = fieldAnalysisPojo.getChartEntity();
        List<String> values = fieldAnalysisPojo.getValues();
        List<String> valuesResult = fieldAnalysisPojo.getValuesResult();
        //协同人团队或负责人团队
        if (EsHelper.isTeamLogicByBusinessType(fieldAttrPojo.getBusinessType()) && !fieldAnalysisPojo.getIsGroupField()) {
            //判断为多表
            if (Objects.equals(chartEntity.getMuti(),0)) {
                for (int i = 0; i < values.size(); i++) {
                    results.put(values.get(i), valuesResult.get(i));
                }
                return results;
            }
            results = fieldAnalysisHelp.teamAnalysis(fieldAnalysisPojo);
        } else {
            results = fieldAnalysisHelp.userAnalysis(fieldAnalysisPojo);
        }
        return results;
    }
}
